#include "serialmanager.h"
#include <QDebug>


SerialManager::SerialManager(QObject *parent)
    : QObject(parent)
{
    connect(&m_serial, &QSerialPort::readyRead, this, &SerialManager::handleReadyRead);

    // 启动 2s 定时器
    connect(&m_timer, &QTimer::timeout, this, &SerialManager::refreshPorts);
    m_timer.start(3000);
    refreshPorts(); // 启动时先刷新一次
}

void SerialManager::refreshPorts()
{
    // QStringList newPorts;
    QVariantList newPorts;

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        // newPorts << info.portName();

        QVariantMap portInfo;
        portInfo[tr("portName")]     = info.portName();
        portInfo[tr("description")]  = info.description();
        portInfo[tr("manufacturer")] = info.manufacturer();
        QString desc = info.description().isEmpty() ? tr("Unknown") : info.description();
        portInfo[tr("displayName")] = QString(tr("%1:%2")).arg(info.portName(), desc);
        newPorts.append(portInfo);
    }
    // m_ports = newPorts;
    // emit availablePortsChanged();

    if (newPorts != m_ports) {
        m_ports = newPorts;
        emit availablePortsChanged();
    }
    // qDebug() << "newPorts:" << newPorts;
    // qDebug() << "m_ports:" << m_ports;
}


// void SerialManager::openPort(const QString &portName, int baudRate)
void SerialManager::openPort()
{
    if (m_serial.isOpen())
        m_serial.close();

    m_serial.setPortName(m_portName);
    m_serial.setBaudRate(m_baudRate);

    if (m_serial.open(QIODevice::ReadWrite)) {
        emit connectedChanged();
    } else {
        qWarning() << "Failed to open port" << m_portName;
    }
}

void SerialManager::closePort()
{
    if (m_serial.isOpen()) {
        m_serial.close();
        emit connectedChanged();
        emit closeSerialmanager();
    }
}

void SerialManager::handleReadyRead()
{
    QByteArray data = m_serial.readAll();
    m_rxCount++;
    emit rxCountChanged();
    emit dataReceived(QString::fromUtf8(data));
}

// void SerialManager::sendData(const QString &data)
// {
//     if (!m_serial.isOpen()) return;
//     QByteArray bytes = data.toUtf8();
//     m_serial.write(bytes);
//     m_txCount++;
//     emit txCountChanged();
// }

void SerialManager::writeData(const QByteArray &data)
{
    if (m_serial.isOpen()) {
        qDebug() << "writeData:" << data;
        m_serial.write(data);
        m_txCount++;
        emit txCountChanged();
        emit dataSend(QString::fromUtf8(data));
    }
}

bool SerialManager::isOpen() const
{
    return m_serial.isOpen();
}

void SerialManager::setPort(const QString &portName)
{
    m_portName = portName;
}

void SerialManager::setBaudRate(int baudRate)
{
    m_baudRate = baudRate;
}

void  SerialManager::setRxCount(const int &cnt) {
    if(m_rxCount != cnt) {
        m_rxCount = cnt;
        emit rxCountChanged();
    }
}

void  SerialManager::setTxCount(const int &cnt) {
    if(m_txCount != cnt) {
        m_txCount = cnt;
        emit txCountChanged();
    }
}
